package com.hbvtc.xxxy.nikeSale.daos;

/*
 * 数据的增删改查
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.hbvtc.xxxy.nikeSale.models.Salesman;
import com.hbvtc.xxxy.nikeSale.utils.DbManager;

/*
 * 员工信息表  管理员的
 */
public class SalesmanDao {
	private static Connection conn = null;
	private static PreparedStatement pst = null;
	private static ResultSet rs = null;

	// 查询 这个是登录界面
	public Salesman getAllSalesman(Object[] obj) throws SQLException {
		Salesman salesman = new Salesman();
		conn = DbManager.getconn();
		String sql = "select * from salesman where SalesmanId=? and Pwd=?";

		pst = conn.prepareStatement(sql);
		pst.setInt(1, Integer.parseInt(obj[0].toString()));
		pst.setString(2, obj[1].toString());
		rs = pst.executeQuery();

		while (rs.next()) {
			salesman.setSalesmanId(rs.getInt("SalesmanId"));
			salesman.setSalesmanName(rs.getString("SalesmanName"));
			salesman.setGender(rs.getString("Gender"));
			salesman.setMobile(rs.getString("Mobile"));
			salesman.setPwd(rs.getString("Pwd"));
			salesman.setRole(rs.getString("Role"));
			salesman.setBaseSalary(rs.getString("BaseSalary"));
		}
		return salesman;
	}

	// 插入内容
	public static void addUser(Salesman salesman) throws SQLException {
		conn = DbManager.getconn();
		String sql = "insert into salesman (SalesmanId,Pwd,SalesmanName,Mobile)values(?,?,?,?)";
		pst = conn.prepareStatement(sql);
		pst.setInt(1, salesman.getSalesmanId());
		pst.setString(2, salesman.getPwd());
		pst.setString(3, salesman.getSalesmanName());
		pst.setString(4, salesman.getMobile());
		pst.execute();// 执行数据库语句
	}

	public boolean getBySalesmanId(int id) throws SQLException {
		String sql = "select SalesmanId from salesman where SalesmanId=?";
		conn = DbManager.getconn();
		pst = conn.prepareStatement(sql);
		pst.setInt(1, id);
		rs = pst.executeQuery();
		int i = 0;
		while (rs.next()) {
			i = rs.getInt("SalesmanId");
		}
		if (i > 0) {
			return false;
		} else {
			return true;
		}
	}

	public void AddSalesman(Salesman goods) throws SQLException {
		conn=DbManager.getconn();
		String sql = "insert into salesman (SalesmanId,SalesmanName,Mobile,Gender,BaseSalary,Role,Pwd)values(?,?,?,?,?,?,?)";
		pst = conn.prepareStatement(sql);
		pst.setInt(1, goods.getSalesmanId());
		pst.setString(2, goods.getSalesmanName());
		pst.setString(3, goods.getMobile());
		pst.setString(4, goods.getGender());
		pst.setString(5, goods.getBaseSalary());
		pst.setString(6, goods.getRole());
		pst.setString(7, goods.getPwd());
		pst.execute();// 执行SQL语句ps
	}

}
